package cn.iocoder.yudao.module.crossborder.platform.service.platformsynctask;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.crossborder.platform.controller.admin.platformsynctask.vo.*;
import cn.iocoder.yudao.module.crossborder.platform.dal.dataobject.platformsynctask.PlatformSyncTaskDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.crossborder.platform.dal.mysql.platformsynctask.PlatformSyncTaskMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.crossborder.platform.enums.ErrorCodeConstants.*;

/**
 * 跨境电商数据同步任务 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PlatformSyncTaskServiceImpl implements PlatformSyncTaskService {

    @Resource
    private PlatformSyncTaskMapper platformSyncTaskMapper;

    @Override
    public Long createPlatformSyncTask(PlatformSyncTaskSaveReqVO createReqVO) {
        // 插入
        PlatformSyncTaskDO platformSyncTask = BeanUtils.toBean(createReqVO, PlatformSyncTaskDO.class);
        platformSyncTaskMapper.insert(platformSyncTask);

        // 返回
        return platformSyncTask.getId();
    }

    @Override
    public void updatePlatformSyncTask(PlatformSyncTaskSaveReqVO updateReqVO) {
        // 校验存在
        validatePlatformSyncTaskExists(updateReqVO.getId());
        // 更新
        PlatformSyncTaskDO updateObj = BeanUtils.toBean(updateReqVO, PlatformSyncTaskDO.class);
        platformSyncTaskMapper.updateById(updateObj);
    }

    @Override
    public void deletePlatformSyncTask(Long id) {
        // 校验存在
        validatePlatformSyncTaskExists(id);
        // 删除
        platformSyncTaskMapper.deleteById(id);
    }

    @Override
        public void deletePlatformSyncTaskListByIds(List<Long> ids) {
        // 删除
        platformSyncTaskMapper.deleteByIds(ids);
        }


    private void validatePlatformSyncTaskExists(Long id) {
        if (platformSyncTaskMapper.selectById(id) == null) {
            throw exception(PLATFORM_SYNC_TASK_NOT_EXISTS);
        }
    }

    @Override
    public PlatformSyncTaskDO getPlatformSyncTask(Long id) {
        return platformSyncTaskMapper.selectById(id);
    }

    @Override
    public PageResult<PlatformSyncTaskDO> getPlatformSyncTaskPage(PlatformSyncTaskPageReqVO pageReqVO) {
        return platformSyncTaskMapper.selectPage(pageReqVO);
    }

}